Namespace Controls
    <Serializable(), ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> _
    Public Class DataGridCell
        Inherits Windows.Forms.DataGridViewTextBoxCell

        Private mAddress As Long
        Private mData() As Byte
        Private mValue As Single = 0
        Private mWidth As Integer = 50

        Private mControlValue As Single
        Private mControlSwitch As Single
        Private mToBeSet As Boolean = False
        Private mHighLight As Boolean = False

        Private mTable As Table

        Sub New()
            MyBase.New()
            Me.Style.SelectionForeColor = Color.White
            Me.Style.Font = My.Settings.Grid_Font
        End Sub
#Region "Properties"
        Public Property toBeSet() As Boolean
            Get
                Return mToBeSet
            End Get
            Set(ByVal value As Boolean)
                mToBeSet = value
                setCell(mValue)
                getCell()
            End Set
        End Property
        Public Property controlBit() As Boolean
            Get
                If getDataValue() >= mControlValue Then
                    Return True
                Else
                    Return False
                End If
            End Get
            Set(ByVal value As Boolean)
                If value Then If Not controlBit Then mValue += mControlSwitch
                If Not value Then If controlBit Then mValue -= mControlSwitch
            End Set
        End Property
        Public Property controlValue() As Double
            Get
                Return mControlValue
            End Get
            Set(ByVal value As Double)
                mControlValue = value
            End Set
        End Property
        Public Property controlSwitch() As Double
            Get
                Return mControlSwitch
            End Get
            Set(ByVal value As Double)
                mControlSwitch = value
            End Set
        End Property
        Public ReadOnly Property Address() As Long
            Get
                Return mAddress
            End Get
        End Property
        Public Property HighLight() As Boolean
            Get
                Return mHighLight

            End Get
            Set(ByVal value As Boolean)
                mHighLight = value
                Me.getCell()
            End Set
        End Property
#End Region

        Friend Sub setData(ByRef Data() As Byte, ByVal Address As Long, ByRef table As BikiSoft.Table)
            mTable = table
            mAddress = Address
            mData = Data
            getCell()
        End Sub
        Private Sub setCell(ByVal Value As Single)
            Dim f_Value As Single = Value
            f_Value -= mTable.Offset
            f_Value /= mTable.Multiplier
            If mTable.preDivider <> 0 Then f_Value = mTable.preDivider / f_Value
            If Me.controlBit Then f_Value += mControlSwitch
            If Me.toBeSet Then
                If Me.controlBit Then
                    f_Value -= Me.controlSwitch
                    Me.toBeSet = False
                Else
                    f_Value += Me.controlSwitch
                    Me.toBeSet = False
                End If
            ElseIf Me.controlBit Then
                If f_Value < Me.controlValue Then
                    f_Value = Me.controlValue
                End If
            Else
                If f_Value >= Me.controlValue Then
                    f_Value = Me.controlValue - 1
                End If
            End If
            f_Value += mTable.mTrim
            If mTable.ByteSize = 1 Then
                mData(mAddress) = iByte(f_Value)
            ElseIf mTable.ByteSize = 2 Then
                mData(mAddress) = HIBYTE(iShort(f_Value))
                mData(mAddress + 1) = LOBYTE(iShort(f_Value))
            ElseIf mTable.ByteSize = 4 Then
                Array.Copy(System.BitConverter.GetBytes(f_Value), 0, mData, mAddress, 4)
            End If
            mValue = f_Value
        End Sub
        Private Function getDataValue() As Object
            Dim intReturn As Integer
            If mTable.ByteSize = 1 Or mTable.ByteSize = 0 Then
                intReturn = mData(mAddress) - mTable.mTrim
            ElseIf mTable.ByteSize = 2 Then
                intReturn = MAKEWORD(mData(mAddress + 1), mData(mAddress)) - mTable.mTrim
            Else
                Return System.BitConverter.ToSingle(mData, mAddress)
            End If
            Return intReturn
        End Function
        Private Sub getCell()
            Dim tm_Value As Double = getDataValue()
            Dim tm_clr As Color
            Dim tm_clrfg As Color
            Dim fSize As Integer

            Select Case (mTable.ByteSize)
                Case 1
                    fSize = 36
                Case 2
                    fSize = 54
                Case 4
                    fSize = 54
            End Select

            Select Case (mTable.Scheme)
                Case DisplayScheme.BK_SCH_LATE
                    Me.Style.Format = "N1"
                    Me.controlSwitch = 0
                    Me.controlValue = 256
                    If tm_Value >= 129 Then
                        tm_clrfg = System.Drawing.ColorTranslator.FromOle(RGB(0, 0, 0))
                        tm_clr = System.Drawing.ColorTranslator.FromOle(RGB(191 - (((tm_Value - 128) / 128) * 77), 220 - (((tm_Value - 128) / 128) * 101), 242 - (((tm_Value - 128) / 128) * 62)))
                    Else
                        tm_clrfg = System.Drawing.ColorTranslator.FromOle(RGB(&HFF, &HFF, &HFF))
                        tm_clr = System.Drawing.ColorTranslator.FromOle(RGB(191 - (((tm_Value) / 128) * 77), 220 - (((tm_Value) / 128) * 101), 242 - (((tm_Value) / 128) * 62)))
                    End If
                Case DisplayScheme.BK_SCH_IGN
                    Me.Style.Format = "N0"
                    Me.controlSwitch = 128
                    Me.controlValue = 128
                    If tm_Value < 128 Then
                        tm_clrfg = System.Drawing.ColorTranslator.FromOle(RGB(0, 0, 0))
                        tm_clr = System.Drawing.ColorTranslator.FromOle(RGB(255 - ((tm_Value / 128) * 120), 180 - ((tm_Value / 128) * 129), 90 - ((tm_Value / 128) * 10)))
                    Else
                        tm_clrfg = System.Drawing.ColorTranslator.FromOle(RGB(&HFF, &HFF, &HFF))
                        tm_clr = System.Drawing.ColorTranslator.FromOle(RGB(215 - (((tm_Value - 128) / 128) * 172), 231 - (((tm_Value - 128) / 128) * 120), 164 - (((tm_Value - 128) / 128) * 116)))
                        tm_Value -= Me.controlSwitch
                    End If
                Case DisplayScheme.BK_SCH_EARLY

                    If mTable.Multiplier Mod 1 <> 0 Then
                        Me.Style.Format = "N1"
                    Else
                        Me.Style.Format = "N0"
                    End If
                    Me.controlSwitch = 192
                    Me.controlValue = 128
                    If tm_Value < 128 Then
                        tm_clrfg = System.Drawing.ColorTranslator.FromOle(RGB(0, 0, 0))
                        tm_clr = System.Drawing.ColorTranslator.FromOle(RGB(191 - (((tm_Value) / 128) * 77), 220 - (((tm_Value) / 128) * 101), 242 - (((tm_Value) / 128) * 62)))
                    Else
                        tm_clrfg = System.Drawing.ColorTranslator.FromOle(RGB(&HFF, &HFF, &HFF))
                        tm_clr = System.Drawing.ColorTranslator.FromOle(RGB(191 - (((tm_Value) / 128) * 77), 220 - (((tm_Value) / 128) * 101), 242 - (((tm_Value) / 128) * 62)))
                        tm_Value -= Me.controlSwitch
                    End If
                Case DisplayScheme.BK_SCH_WORD
                    If Me.mTable.Multiplier > 1 Then
                        Me.Style.Format = "N0"
                    Else
                        Me.Style.Format = "N1"
                    End If
                    tm_clrfg = System.Drawing.ColorTranslator.FromOle(RGB(0, 0, 0))
                    tm_clr = System.Drawing.ColorTranslator.FromOle(RGB(191 - (((tm_Value) / 65535) * 77), 220 - (((tm_Value) / 65535) * 101), 242 - (((tm_Value) / 65535) * 62)))
                Case DisplayScheme.BK_SCH_BYTE
                    If mTable.Multiplier Mod 1 <> 0 Then
                        Me.Style.Format = "N1"
                    Else
                        Me.Style.Format = "N0"
                    End If
                    tm_clrfg = System.Drawing.ColorTranslator.FromOle(RGB(0, 0, 0))
                    tm_clr = System.Drawing.ColorTranslator.FromOle(RGB(215 - (((tm_Value - 128) / 128) * 172), 231 - (((tm_Value - 128) / 128) * 120), 164 - (((tm_Value - 128) / 256) * 116)))
                Case DisplayScheme.BK_SCH_SINGLE
                    tm_clrfg = System.Drawing.ColorTranslator.FromOle(RGB(255, 255, 255))
                    tm_clr = Color.Maroon
                    Me.Style.Format = "N3"
                    Me.controlValue = Single.MaxValue
                Case Else
                    Me.Style.Format = ""
            End Select

            If mHighLight Then
                tm_clr = System.Drawing.ColorTranslator.FromOle(RGB(tm_clr.B, tm_clr.G, tm_clr.R))
            End If

            If mTable.preDivider <> 0 Then tm_Value = mTable.preDivider / tm_Value
            tm_Value *= mTable.Multiplier
            tm_Value += mTable.Offset

            Me.mWidth = fSize
            Me.mValue = tm_Value
            MyBase.Value = tm_Value
            Me.Style.BackColor = tm_clr
            Me.Style.ForeColor = tm_clrfg
        End Sub

        Public Shadows Property Value() As Single
            Get
                Return Me.mValue
            End Get
            Set(ByVal value As Single)
                setCell(value)
                mValue = value
                MyBase.Value = mValue
                getCell()
            End Set
        End Property
        Public Overrides Function ToString() As String
            Return Format(mValue, Me.Style.Format)
        End Function

        Protected Overrides Sub Paint(ByVal graphics As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle, ByVal cellBounds As System.Drawing.Rectangle, ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates, ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String, ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle, ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle, ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
            'graphics.TextRenderingHint = Text.TextRenderingHint.AntiAliasGridFit
            graphics.TextContrast = 8
            MyBase.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, mValue, ToString, errorText, cellStyle, advancedBorderStyle, paintParts)
            ' graphics.TextRenderingHint = Text.TextRenderingHint.SystemDefault
        End Sub

        Public Shadows ReadOnly Property ValueType() As System.Type
            Get
                Return GetType(String)
            End Get
        End Property

        Public Shadows ReadOnly Property PreferredSize() As Size
            Get
                Return MeasureTextSize(Me.DataGridView.CreateGraphics, Me.ToString, Me.Style.Font, TextFormatFlags.VerticalCenter)
            End Get
        End Property

    End Class
End Namespace
